package com.example.algorithm.algorithmService;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.algorithm.mapper.SvYdScreenV2Mapper;
import com.example.algorithm.pojo.Department;
import com.example.algorithm.pojo.SvYdScreenV2;
import com.example.algorithm.utils.AreaJudgeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
@Transactional
public class IsActiveService {

    @Autowired
    private SvYdScreenV2Mapper svYdScreenV2Mapper;

    /**
     * 添加是否主被动
     */
    @Transactional
    public void addActive(){
        List<SvYdScreenV2> svYdScreenV2s = selectSvYdScreenV2List();
        if (svYdScreenV2s != null) {
            svYdScreenV2s.forEach(e -> {
                if (getProposeWeight(e) > getDutyWeight(e)) {
                    e.setActiveOrNot("被动");
                } else {
                    e.setActiveOrNot("主动");
                }
                updateActiveOrNot(e);
            });
        }
    }


    /**
     * 设置ActiveOrNot字段值
     * @param svYdScreenV2
     */
    public void updateActiveOrNot(SvYdScreenV2 svYdScreenV2){
        LambdaUpdateWrapper<SvYdScreenV2> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SvYdScreenV2::getId,svYdScreenV2.getId())
                .set(SvYdScreenV2::getActiveOrNot, svYdScreenV2.getActiveOrNot());
        svYdScreenV2Mapper.update(null, wrapper);
    }

    /**
     * 查询所有的 SvYdScreenV2
     * @return
     */
    public List<SvYdScreenV2> selectSvYdScreenV2List(){
        try {
            List<SvYdScreenV2> svYdScreenV2s = svYdScreenV2Mapper.selectList(null);
            return svYdScreenV2s;
        } catch (Exception e) {
            log.info("查询SvYdScreenV2出错",e);
            return null;
        }
    }


    //得到提出单位的权重
    public Integer getProposeWeight(SvYdScreenV2 dataInfo) {
        ArrayList<Integer> weightList = new ArrayList<>();
        String[] split = dataInfo.getPutUnit().replaceAll(",", "、").replaceAll("，", "、").split("、");
        if (split.length > 1) {
            for (int i = 0; i < split.length; i++) {
                if (split[i].length() < 1) {
                    continue;
                }
                Department departMent = AreaJudgeUtils.getDepartMent(split[i]);
                weightList.add(departMent.getWeight());
            }
            weightList.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            return weightList.get(0);
        } else if (split.length == 1) {
            Department departMent = AreaJudgeUtils.getDepartMent(split[0]);
            return departMent.getWeight();
        } else {
            return 0;
        }
    }

    //得到责任单位的权重
    public Integer getDutyWeight(SvYdScreenV2 dataInfo) {
        ArrayList<Integer> weightList = new ArrayList<>();
        String[] split = dataInfo.getDutyUnit().replaceAll(",", "、").replaceAll("，", "、").split("、");
        if (split.length > 1) {
            for (int i = 0; i < split.length; i++) {
                if (split[i].length() < 1) {
                    continue;
                }
                Department departMent = AreaJudgeUtils.getDepartMent(split[i]);
                weightList.add(departMent.getWeight());
            }
            weightList.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            return weightList.get(0);
        } else if (split.length == 1) {
            Department departMent = AreaJudgeUtils.getDepartMent(split[0]);
            return departMent.getWeight();
        } else {
            return 0;
        }
    }


}
